<?php

namespace Helpers;

class AuthLogin {
	const EXPIRE = 10; /**授权链接失效时间**/
	const KEY = 'cd672b288e955665f24d888e61b3a9ee';
	public $appid;

	/** 初始化
	 * @param String $prefix     cookie prefix
	 * @param int    $expire     过期时间
	 * @param String $securekey  cookie secure key
	 */
	public function __construct($appid=''){
		is_string($appid) && $this->appid = $appid;
	}

	function verifyReturn($data=[]){
		$param = [
			'userid' => $data['userid'],
			'appid' => $data['appid'],
			'expiretime' => $data['expiretime'],
			'noncestr' =>  $data['noncestr'],
		];
		return $this->md5Verify(md5(http_build_query($this->argSort($param))),$data['sign'],self::KEY);
	}

	public function runLinkstring($goUrl='',$data=[]){
		$param= [
			'userid' => $data['userid'],
			'appid' => $this->appid,
			'noncestr' => self::randpw(),
			'expiretime' => time()+self::EXPIRE,
		];
		$sign = $this->md5Sign(md5(http_build_query($this->argSort($param))), self::KEY);
		$parameter = $this->createLinkstring(array_merge($param,['sign'=>$sign]));
		return $goUrl.$parameter;
	}

	/**
	 * @param int $len 长度
	 * @param string $format  类型
	 * @return string  返回随机字符串
	 */
	static public function randpw($len=8,$format='ALL'){
		$is_abc = $is_numer = 0;
		$password = $tmp ='';
		switch($format){
			case 'ALL':
				$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
				break;
			case 'CHAR':
				$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
				break;
			case 'NUMBER':
				$chars='0123456789';
				break;
			default :
				$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
				break;
		}
		mt_srand((double)microtime()*1000000*getmypid());
		while(strlen($password)<$len){
			$tmp =substr($chars,(mt_rand()%strlen($chars)),1);
			if(($is_numer <> 1 && is_numeric($tmp) && $tmp > 0 )|| $format == 'CHAR'){
				$is_numer = 1;
			}
			if(($is_abc <> 1 && preg_match('/[a-zA-Z]/',$tmp)) || $format == 'NUMBER'){
				$is_abc = 1;
			}
			$password.= $tmp;
		}
		if($is_numer <> 1 || $is_abc <> 1 || empty($password) ){
			$password = self::randpw($len,$format);
		}
		return $password;
	}

	/**
	 * 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
	 * @param $para 需要拼接的数组
	 * return 拼接完成以后的字符串
	 */
	public function createLinkstring($para) {
		$arg  = "";
		while (list ($key, $val) = each ($para)) {
			$arg.=$key."=".$val."&";
		}
		//去掉最后一个&字符
		$arg = substr($arg,0,count($arg)-2);

		//如果存在转义字符，那么去掉转义
		if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}

		return $arg;
	}

	/**
	 * 签名字符串
	 * @param $prestr 需要签名的字符串
	 * @param $key 私钥
	 * return 签名结果
	 */
	public function md5Sign($prestr, $key) {
		$prestr = $prestr . $key;
		return md5($prestr);
	}

	/**
	 * 验证签名
	 * @param $prestr 需要签名的字符串
	 * @param $sign 签名结果
	 * @param $key 私钥
	 * return 签名结果
	 */
	public function md5Verify($prestr, $sign, $key) {
		$prestr = $prestr . $key;
		$mysgin = md5($prestr);

		if($mysgin == $sign) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * 对数组排序
	 * @param $para 排序前的数组
	 * return 排序后的数组
	 */
	public function argSort($para) {
		ksort($para);
		reset($para);
		return $para;
	}
}

//$authLogin =  new AuthLogin('15058885');
//echo $authLogin->runLinkstring('http://zt.yunzuoye.net:84/apps/course/resource/web/index/authlogin?',['userid'=>152]);
//var_dump( $authLogin->verifyReturn($_GET) );

